/**
 * 日期格式化
 * @param time
 * @returns {*}
 */
function formatDate(time) {
    if (time == '' || time == null || time == undefined) return '';
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}

/**
 * 日期格式化模板
 * @param time: 要操作的时间
 * 		  type: 1 样式为"2016-09-05 00:00"
 * @returns {*}
 */
function formatDate(time,type) {
    if (time == '' || time == null || time == undefined) return '';
    var datetime = new Date();
    datetime.setTime(time);
    var year = datetime.getFullYear();
    var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
    var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
    var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
    var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
    var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
    if(type == 1){
    	return year + "-" + month + "-" + date + " " + hour + ":" + minute;
    }else if(type == 2){
    	return year + "-" + month + "-" + date;
    }else{
    	return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
    }
}

function stateUtil(obj){
	if(typeof obj=='string'){
		obj = JSON.parse(obj);
	}
	if(obj.code===-1){
		layer.msg(obj.msg, {icon: obj.icon});
		setTimeout(function(){
			location.href=BaseUrl+"/manger/main";
		},1000)
		return false;
	}else if(obj.code==="0"){
		layer.msg(obj.msg, {icon: obj.icon});
		return true;
	}else{
		setTimeout(function(){
			layer.msg(obj.msg, {icon: obj.icon});
		},500)
		return false;
	}
}

/**
 * 默认单选框选中 适合 数据为 1 0
 * @param b
 * @param name
 * @returns
 */
function fillRadio(b, name) {
    if(b) {
        $(":radio[name='"+name+"'][value='1']").iCheck('check');
    }else{
        $(":radio[name='"+name+"'][value='0']").iCheck('check');
    }
}

/**
 * 下拉框选中
 * @param id
 * @param data
 * @returns
 */
function fillSelect(id,data){
	$("#"+id+" option[value='"+data+"']").attr("selected","selected");
}

/**
 * 单选框选中
 * @param name
 * @param data
 * @returns
 */
function radioCheck(name,data){
	$(":radio[name='"+name+"'][value='"+data+"']").iCheck('check');
}

/**
 * 获取URL值
 * @param name
 * @returns
 */
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return unescape(r[2]); return null; //返回参数值
}


/**
 * 模板解析器
 * @param html
 * @param options
 * @returns
 */
function templateEngine(html, options) {
    var re = /<!([^!>]+)?!>/g,
        reExp = /(^( )?(if|for|else|switch|case|formatDate|break|{|}))(.*)?/g,
        code = 'var r=[];\n',
        cursor = 0;
    var add = function(line, js) {
        js ? (code += line.match(reExp) ? line + '\n' : 'r.push(' + line + ');\n') :
            (code += line != '' ? 'r.push("' + line.replace(/"/g, '\\"') + '");\n' : '');
        return add;
    }
    while (match = re.exec(html)) {
        add(html.slice(cursor, match.index))(match[1], true);
        cursor = match.index + match[0].length;
    }
    add(html.substr(cursor, html.length - cursor));
    code += 'return r.join("");';
    return new Function(code.replace(/[\r\t\n]/g, '')).apply(options);
}

/**
 * 更新chosen数据
 * @param id
 */
function updateChosen(id){
	$("#"+id).trigger("chosen:updated");
}


jQuery.validator.addMethod("regex",
    function(value,element,params){
        var exp = new RegExp(params);
        return exp.test(value);
},"格式错误");